package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.quwan.common.enums.RequestUriEnum;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.constans.CacheKey;
import com.quwan.entity.TMemberClickNum;
import com.quwan.manager.service.MemberClickNumService;
import com.quwan.mapper.TMemberClickNumMapper;
import com.quwan.utils.DateUtils;
import com.quwan.utils.Parse;
import com.quwan.utils.redis.Redis;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author laozhang
 * @date 2022/05/28
 */
@Service
@RequiredArgsConstructor
public class MemberClickNumServiceImpl implements MemberClickNumService {
    private final TMemberClickNumMapper memberClickNumMapper;
    private  final Redis redis;

    @Override
    public PageResult<TMemberClickNum> getMemberClickNumListPage(int page, int size, String beginTime, String endTime) {
        LambdaQueryWrapperX<TMemberClickNum> wrapper = new LambdaQueryWrapperX<TMemberClickNum>()
                .orderByDesc(TMemberClickNum::getCreateTime);
        if (StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberClickNum::getCreateTime, beginTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberClickNum::getCreateTime, endTime + " 23:59:59");
        }

        Page<TMemberClickNum> memberAskPage = memberClickNumMapper.selectPage(new Page<>(page, size), wrapper);
        List<TMemberClickNum> reslist = memberAskPage.getRecords();
        PageResult<TMemberClickNum> result = new PageResult<TMemberClickNum>()
                .setCurrent(memberAskPage.getCurrent())
                .setPages(memberAskPage.getPages())
                .setSize(memberAskPage.getSize())
                .setTotal(memberAskPage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    public List<TMemberClickNum> getMemberClickNumList(String beginTime, String endTime) {
        LambdaQueryWrapperX<TMemberClickNum> wrapper = new LambdaQueryWrapperX<TMemberClickNum>()
                .orderByDesc(TMemberClickNum::getCreateTime);
        if (StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberClickNum::getCreateTime, beginTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberClickNum::getCreateTime, endTime + " 23:59:59");
        }
        List<TMemberClickNum> reslist = memberClickNumMapper.selectList(wrapper);
        return reslist;
    }

    @Override
    public TMemberClickNum selectTMemberClickNumByCurrentTime() {
        LambdaQueryWrapperX<TMemberClickNum> wrapper = new LambdaQueryWrapperX<TMemberClickNum>();
        wrapper.eq(TMemberClickNum::getCreateTime, LocalDate.now());
        return memberClickNumMapper.selectOne(wrapper);
    }

    @Override
    public boolean insertTMemberClickNum(TMemberClickNum memberClickNum) {
        return memberClickNumMapper.insert(memberClickNum) == 1;
    }

    @Override
    public boolean updateTMemberClickNum(TMemberClickNum memberClickNum) {
        return memberClickNumMapper.updateById(memberClickNum) == 1;
    }

    @Override
    public void autoAccessCount(String timeKey) {
        Integer count = memberClickNumMapper.selectCount(new QueryWrapper<>(new TMemberClickNum().setCreateTime(LocalDate.now())));
        if (count == 0) {
            Map<Object, Object> map = redis.hIncryGetAll(CacheKey.DATA.accessCount + timeKey);
            Map<Integer, Integer> countMap = map.entrySet().stream().collect(Collectors.toMap(e->Parse.toInt(e.getKey()), e->Parse.toInt(e.getValue())));
            TMemberClickNum insert = new TMemberClickNum()
                    .setCircleNum(countMap.get(RequestUriEnum.CIRCLE.getType()))
                    .setCityNum(countMap.get(RequestUriEnum.PUBLISH_ORDER_LIST_SAME.getType()))
                    .setContentDetailNum(countMap.get(RequestUriEnum.PUBLISH_ORDER_DETAIL.getType()))
                    .setCreateTime(DateUtils.localDateOfStr(timeKey))
                    .setExtendNum(countMap.get(RequestUriEnum.SPREAD.getType()))
                    .setFinishDetailNum(countMap.get(RequestUriEnum.PUBLISH_FINISH_DETAIL.getType()))
                    .setFinishNum(countMap.get(RequestUriEnum.PUBLISH_FINISH.getType()))
                    .setHandleDetailNum(countMap.get(RequestUriEnum.AWAIT_DETAIL.getType()))
                    .setHandleNum(countMap.get(RequestUriEnum.AWAIT_PROCESS.getType()))
                    .setMessageNum(countMap.get(RequestUriEnum.MSG.getType()))
                    .setMoneyNum(countMap.get(RequestUriEnum.BALANCE.getType()))
                    .setNavDetailNum(countMap.get(RequestUriEnum.PUBLISH_ORDER_LIST_SAME.getType()))
                    .setMyNum(countMap.get(RequestUriEnum.ME.getType()))
                    .setOrderPageNum(countMap.get(RequestUriEnum.BUYIT.getType()))
                    .setPageNum(countMap.get(RequestUriEnum.MEMBER_HOME.getType()))
                    .setRealNameNum(countMap.get(RequestUriEnum.REAL_AUTH.getType()))
                    .setRunDetailNum(countMap.get(RequestUriEnum.PROCESSING_DETAIL.getType()))
                    .setRunNum(countMap.get(RequestUriEnum.PROCESSING.getType()))
                    .setSkillNum(countMap.get(RequestUriEnum.PUBLISH_ORDER.getType()))
                    .setSquareNum(countMap.get(RequestUriEnum.DISPATCH_LIST.getType()));
            if (memberClickNumMapper.insert(insert)==1)
                redis.del(CacheKey.DATA.accessCount + timeKey);




        }

    }

}
